package com.web.task;

import com.web.dao.*;
import com.web.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.web.manager.CoreManager.countTotalVal;
import static java.math.BigDecimal.ROUND_FLOOR;
import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 *
 * @author FDjavaone
 * @date 2017/9/18
 */

@Component
@Configurable
@EnableScheduling
@Slf4j
public class AtomicMskBuyOrderDealJob {
    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private MskBuyOrderMapper mskBuyOrderMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private MskDetailMapper mskDetailMapper;
    @Autowired
    private CoreDetailMapper coreDetailMapper;
    @Autowired
    private CompanyCompanyMsDetailMapper companyCompanyMsDetailMapper;
    @Autowired
    private CompanyMarketDetailMapper companyMarketDetailMapper;
    @Autowired
    private CompanyMarketMsDetailMapper companyMarketMsDetailMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CompanyReportMapper companyReportMapper;


    @Scheduled(cron = "*/10 * * * * ?")
    public void atomicBuyMsk(){

        //获取未交易完成的msk买入挂单且需要优先挂买的
        MskBuyOrder mskBuyOrder=mskBuyOrderMapper.selectUnDealAndFirst();

        //获取未交易完成的msk买入挂单
        List<MskBuyOrder> mskBuyOrders=mskBuyOrderMapper.selectUnDealOrder();
        if(mskBuyOrder!=null){
            mskBuyOrders.add(0,mskBuyOrder);
        }

        for(MskBuyOrder msk:mskBuyOrders){
            Date date=new Date();


            //获取当前平台信息
            Platform platform = platformMapper.selectByPrimaryKey(1L);

            //用户账户信息
            Account account=accountMapper.selectByMid(msk.getmId());
            Member member=memberMapper.selectByPrimaryKey(msk.getmId());
            BigDecimal newPrice=new BigDecimal("0");

            //msk当前市场价格
            if(platform.getMarketMsk().compareTo(new BigDecimal("0"))==0){
                newPrice=new BigDecimal("1");
            }else{
                newPrice = platform.getMarketFundPool().divide(platform.getMarketMsk(),2,ROUND_HALF_DOWN);
            }

            if (newPrice.compareTo(new BigDecimal("2")) != -1) {
                log.info("裂变保护期，挂买交易停止");
                break;
            }else {
                int availableNum=msk.getCorePoints().divide(newPrice,10,ROUND_FLOOR).intValue();

                if(platform.getCompanyMsk().intValue()>=availableNum){
                    //公司msk账户减少对应数量，市场msk增加对应数量，用户的msk增加对应数量，挂买单变成已完成，
                    // 购买msk剩余的睿积分返还到用户睿积分账户，花掉的睿积分进入到市场资金池
                    platformMapper.companyMskSubtract(new BigDecimal(availableNum),1L);
                    CompanyCompanyMsDetail companyCompanyMsDetail=new CompanyCompanyMsDetail();
                    companyCompanyMsDetail.setCreateTime(date);
                    companyCompanyMsDetail.setModifyTime(date);
                    companyCompanyMsDetail.setContent("MS挂买单"+msk.getId()+"自动挂买,公司MS减少");
                    companyCompanyMsDetail.setDetail("-"+availableNum);
                    companyCompanyMsDetail.setBal(platformMapper.selectByPrimaryKey(1L).getCompanyMsk().toString());
                    companyCompanyMsDetail.setDescription("MS挂买单"+msk.getId()+"自动挂买,公司MS减少");
                    companyCompanyMsDetail.setFromNum(String.valueOf(msk.getId()));
                    companyCompanyMsDetail.setName(member.getName());
                    companyCompanyMsDetail.setPhone(member.getPhone());
                    companyCompanyMsDetailMapper.insertSelective(companyCompanyMsDetail);


                    platformMapper.marketMskAdd(availableNum,1L);
                    CompanyMarketMsDetail companyMarketMsDetail=new CompanyMarketMsDetail();
                    companyMarketMsDetail.setCreateTime(date);
                    companyMarketMsDetail.setModifyTime(date);
                    companyMarketMsDetail.setContent("MS挂买单"+msk.getId()+"自动挂买，市场MS增加");
                    companyMarketMsDetail.setDetail("+"+availableNum);
                    companyMarketMsDetail.setBal(platformMapper.selectByPrimaryKey(1L).getMarketMsk().toString());
                    companyMarketMsDetail.setDescription("MS挂买单"+msk.getId()+"自动挂买，市场MS增加");
                    companyMarketMsDetail.setFromNum(String.valueOf(msk.getId()));
                    companyMarketMsDetail.setName(member.getName());
                    companyMarketMsDetail.setPhone(member.getPhone());
                    companyMarketMsDetailMapper.insertSelective(companyMarketMsDetail);

                    CompanyReport companyReport=new CompanyReport();

                    companyReport.setCreateTime(date);
                    companyReport.setModifyTime(date);
                    companyReport.setReserve1("MS自动挂买");

                    BigDecimal marketRecord=new BigDecimal("0");
                    BigDecimal coreRecord=new BigDecimal("0");
                    //市场资金池增加
                    companyReport.setMarketFund("+" + new BigDecimal(availableNum).multiply(newPrice));
                    marketRecord = new BigDecimal(companyReport.getMarketFund());
                    companyReport.setMarketFund((companyReport.getMarketFund()!=null?companyReport.getMarketFund():"") + "\r\n=" + platformMapper.selectByPrimaryKey(1L).getMarketFundPool().add(marketRecord));


                    platformMapper.marketFundPoolAdd(new BigDecimal(availableNum).multiply(newPrice),1L);
                    CompanyMarketDetail companyMarketDetail=new CompanyMarketDetail();
                    companyMarketDetail.setCreateTime(date);
                    companyMarketDetail.setModifyTime(date);
                    companyMarketDetail.setContent("MS挂买单"+msk.getId()+"自动挂买，睿积分流入市场资金池");
                    companyMarketDetail.setDetail("+"+new BigDecimal(availableNum).multiply(newPrice));
                    companyMarketDetail.setName(member.getName());
                    companyMarketDetail.setPhone(member.getPhone());
                    companyMarketDetail.setFromNum(String.valueOf(msk.getId()));
                    companyMarketDetail.setBal(platformMapper.selectByPrimaryKey(1L).getMarketFundPool().toString());
                    companyMarketDetail.setDescription("MS挂买单"+msk.getId()+"自动挂买，睿积分流入市场资金池");

                    companyMarketDetailMapper.insertSelective(companyMarketDetail);


                    accountMapper.mskAdd(availableNum,msk.getmId());
                    if(availableNum!=0){
                        MskDetail mskDetail=new MskDetail();
                        mskDetail.setTransactionNum(new BigDecimal(availableNum));
                        mskDetail.setTransactionPrice(newPrice);
                        mskDetail.setModifyTime(date);
                        mskDetail.setCreateTime(date);
                        mskDetail.setType(1);
                        mskDetail.setContent("买入");
                        mskDetail.setDetail("MS:+" +availableNum);
                        mskDetail.setaId(account.getId());
                        mskDetail.setBal(accountMapper.selectByPrimaryKey(account.getId()).getMsk().toString());
                        mskDetail.setName(member.getName());
                        mskDetail.setPhone(member.getPhone());
                        mskDetailMapper.insertSelective(mskDetail);
                    }
                    //剩余睿积分归还
                    companyReport.setCoreBal("+"+msk.getCorePoints().subtract(new BigDecimal(availableNum).multiply(newPrice)));
                    coreRecord = new BigDecimal(companyReport.getCoreBal());
                    companyReport.setCoreBal(companyReport.getCoreBal() + "\r\n=" + accountMapper.countCoreNum().add(coreRecord));

                    companyReport.setTotal(marketRecord.add(coreRecord)+"\r\n="+countTotalVal(companyReport));

                    companyReportMapper.insertSelective(companyReport);

                    accountMapper.corePointsAdd(msk.getCorePoints().subtract(new BigDecimal(availableNum).multiply(newPrice)),account.getId());
                    CoreDetail coreDetail=new CoreDetail();
                    coreDetail.setModifyTime(date);
                    coreDetail.setCreateTime(date);
                    coreDetail.setContent("买入MS剩余");
                    coreDetail.setDetail("+" + msk.getCorePoints().subtract(new BigDecimal(availableNum).multiply(newPrice)));
                    coreDetail.setaId(account.getId());
                    coreDetail.setBal(accountMapper.selectByPrimaryKey(account.getId()).getCorePoints().toString());
                    coreDetail.setName(member.getName());
                    coreDetail.setPhone(member.getPhone());
                    coreDetail.setFromNum(String.valueOf(msk.getId()));

                    coreDetailMapper.insertSelective(coreDetail);

                    msk.setModifyTime(date);
                    msk.setStatus(2);
                    msk.setDealNum(availableNum);
                    msk.setDealPrice(newPrice);
                    msk.setDealTime(date);
                    mskBuyOrderMapper.updateByPrimaryKeySelective(msk);

                }else{
                    log.info("公司ms账户ms数量不足，挂买交易停止");
                    break;
                }
            }


        }


    }
}
